﻿/**************************************************************************************************************
 * Class name:  Numeric.cs
 * Description: 数字助手
 * Author:      Ju2ender
 * Create:      2012-10-22 16:01:05
 * Update:      2013-03-19 15:28:04
 **************************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DearLee.Toolkit {

  public class Numeric {

    /// <summary>
    /// 验证数字
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>true：是数字，false：不是数字</returns>
    public static bool CheckNumeric(string str) {

      Regex reg = null;
      reg = new Regex(@"^[-]?\d+[.]?\d*$");
      return reg.IsMatch(str);
    }

    public static char[] constant = { 
      
      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
      'a', 'b', 'c', 'd', 'e', 'f' ,'g', 'h', 'i', 'j', 
      'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
      'u', 'v', 'w', 'x', 'y', 'z',
      'A', 'B', 'C', 'D', 'E', 'F' ,'G', 'H', 'I', 'J',
      'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
      'U', 'V', 'W', 'X', 'Y', 'Z' , 
      '_'
    };

    /// <summary>
    /// 生成随即码，内容包括数字、大小写英文字母、下划线
    /// </summary>
    /// <param name="length">密码长度</param>
    /// <returns>随机6位密码</returns>
    public static string GenerateRandomCode(int length) {

      StringBuilder sCode = null;
      Random rdm = null;

      sCode = new StringBuilder(constant.Length);
      rdm = new Random();

      for (int i = 0; i < length; i++) {
        sCode.Append(constant[rdm.Next(constant.Length)]);
      }

      return sCode.ToString();
    }

    /// <summary>
    /// 生成随即码，内容包括数字、大小写英文字母、下划线
    /// </summary>
    /// <param name="length">密码长度</param>
    /// <returns>随机6位密码</returns>
    public static List<string> GenerateRandomCode(int length, int count) {

      StringBuilder sCode = null;
      Random rdm = null;
      List<string> list = null;

      list = new List<string>();

      for (int i = 0; i < count; i++) {
        sCode = new StringBuilder(constant.Length);
        rdm = new Random();

        for (int j = 0; j < length; j++) {
          sCode.Append(constant[rdm.Next(constant.Length)]);
        }

        list.Add(sCode.ToString());
      }

      return list;
    }
  }
}
